package cn.stylefeng.guns.modular.course.controller;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.course.entity.Category;
import cn.stylefeng.guns.modular.course.service.CategoryService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 控制器
 *
 * @author 唐元鹏
 * @Date 2021-02-24 17:02:36
 */
@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {

    private String PREFIX = "/category";

    @Autowired
    private CategoryService categoryService;

    /**
     * 跳转到主页面
     *
     * @author 唐元鹏
     * @Date 2021-02-24
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/category.html";
    }

    /**
     * 新增页面
     *
     * @author 唐元鹏
     * @Date 2021-02-24
     */
    @RequestMapping("/add")
    public String add(Model model) {
        Category category=new Category();
        category.setPid(0);
        //获取一级分类
        List<Category> categoryList=categoryService.findListBySpec(category);
        model.addAttribute("category",categoryList);

        return PREFIX + "/category_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 唐元鹏
     * @Date 2021-02-24
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        Category category=new Category();
        category.setPid(0);

        //获取一级分类
        List<Category> categoryList=categoryService.findListBySpec(category);
        model.addAttribute("category",categoryList);
        return PREFIX + "/category_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 唐元鹏
     * @Date 2021-02-24
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(Category categoryParam) {
        this.categoryService.add(categoryParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 唐元鹏
     * @Date 2021-02-24
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(Category categoryParam) {
        this.categoryService.update(categoryParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 唐元鹏
     * @Date 2021-02-24
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(Category categoryParam) {
        this.categoryService.delete(categoryParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 唐元鹏
     * @Date 2021-02-24
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(Category categoryParam) {
        Category detail = this.categoryService.getById(categoryParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 唐元鹏
     * @Date 2021-02-24
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(Category categoryParam) {
        return this.categoryService.findPageBySpec(categoryParam);
    }



    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.categoryService.getTree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }
}


